package kz.alamty.dev.test;

import static org.junit.Assert.*;

import java.util.List;

import kz.alamty.dev.dao.AnswerDAO;
import kz.alamty.dev.dao.QuestionDAO;
import kz.alamty.dev.obj.Answer;
import kz.alamty.dev.obj.Question;
import kz.alamty.dev.obj.User;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AnswerDaotest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	private kz.alamty.dev.obj.Answer answer;
	private Question question;
	private User user;
	private AnswerDAO asdao;
	private QuestionDAO qdao;

	@Before
	public void setUp() throws Exception {
		asdao = new AnswerDAO();
		qdao = new QuestionDAO();
		user = new User("me", "123456");
		question = new Question(user,"Green is yours like Color?");
		answer = new Answer(question, user, "0");
		
	}

	@After
	public void tearDown() throws Exception {
		asdao = null;
		qdao =null;
	}

	@Test
	public final void testSave() {
		Session session = this.asdao.getSession();
		Transaction trans = null;
        try {
			trans = session.beginTransaction();
			session.save(user);	
			session.save(question);
			session.save(answer);
			trans.commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			trans.rollback();
			e.printStackTrace();
			assertTrue(false);
		}finally{
			session.close();
		}
        
        
	}

	@Test
	public final void testQuesitonById()
	{
		Question q = new  Question();
		q = qdao.findById(new Long(7));
		System.out.println(q.getText()+"{"+q.getOwner().getLogin()+"["+q.getOwner().getId());
		
	}
	
	@Test
	public final void testFindExample()
	{
		Question q = new Question();
		q.setId(new Long("4"));
		
		Answer answer1 = new Answer();
		answer1.setQuestion(q);
		answer1.setChoose("0");
		List list = asdao.findByExample(answer1);
		System.out.println("this is the size="+list.size());
		
	}
	
	@Test
	public final void testDelete() {
		try {
			asdao.delete(answer);
			assertTrue("condition",true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
	}



	@Test
	public final void testFindAll() {
		try {
			List list =asdao.findAll();
			assertTrue("list is :"+list.size(),true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
	}
	
	@Test
	public final void testFindAllQuestion(){
		try {
			List list = qdao.findAll();
			assertTrue("list is :"+list.size(),true);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			assertTrue(false);
		}
	}



}
